package com.eightbears.beboy.base.view.popupwindow;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.eightbears.beboy.R;
import com.eightbears.beboy.base.view.ListItemDecoration;
import com.eightbears.beboy.base.view.basepopu.BasePopupWindow;
import com.eightbears.beboy.base.view.layout.MyLinearLayoutManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Bao on 2017/5/3.
 */
public class MessageAddPopup extends BasePopupWindow implements View.OnClickListener {

    public static final String ITEM_ADD_FRIEND = "1";
    public static final String ITEM_CREATE_GROUP = "2";
    public static final String ITEM_QC_CODE = "3";

    private View popupView;
    private RecyclerView rv_list;

    private OnClickItemListener mOnClickItemListener;

    public MessageAddPopup(Activity context) {
        super(context);
        bindEvent();
    }

    @Override
    public Animation getAnimation() {
        return getDefaultAlphaAnimation();
    }

    @Override
    public AnimationSet getAnimationSet() {
        return null;
    }

    @Override
    public View getInputView() {
        return null;
    }

    @Override
    public View getDismissView() {
        return popupView.findViewById(R.id.click_to_dismiss);
    }

    @Override
    public View getPopupView() {
        popupView = LayoutInflater.from(mContext).inflate(R.layout.view_popup_message_add, null);
        return popupView;
    }

    @Override
    public View getAnimaView() {
        return popupView.findViewById(R.id.popup_anima);
    }

    private void bindEvent() {
        if (popupView != null) {
            rv_list = (RecyclerView) popupView.findViewById(R.id.rv_list);
            initEvent();
        }
    }

    private void initEvent() {
        AddItemAdapter addItemAdapter = new AddItemAdapter();
        ListItemDecoration listItemDecoration = new ListItemDecoration(mContext, 1, R.color.line_color);
        listItemDecoration.setColor(R.color.line_color);
        rv_list.setLayoutManager(new MyLinearLayoutManager(mContext));
        rv_list.addItemDecoration(listItemDecoration);
        rv_list.setAdapter(addItemAdapter);

        addItemAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ItemMenu itemMenu = (ItemMenu) adapter.getItem(position);
                mOnClickItemListener.onClickItem(itemMenu.getId());
            }
        });
    }

    @Override
    public void onClick(View v) {
        dismiss();
    }

    public interface OnClickItemListener {
        void onClickItem(String Type);
    }

    public void setOnClickItemListener(OnClickItemListener onClickItemListener) {
        this.mOnClickItemListener = onClickItemListener;
    }


    private class AddItemAdapter extends BaseQuickAdapter<ItemMenu, BaseViewHolder> {

        public AddItemAdapter() {
            super(R.layout.item_message_add, ItemMenu.getItem());
        }

        @Override
        protected void convert(BaseViewHolder helper, ItemMenu item) {
            helper.setText(R.id.tv_name, item.getItemName())
                    .addOnClickListener(R.id.ll_root);
        }
    }


    static class ItemMenu {


        private String itemName;
        private String id;

        public ItemMenu(String itemName, String id){
            this.itemName = itemName;
            this.id = id;
        }

        public String getItemName() {
            return itemName;
        }

        public void setItemName(String itemName) {
            this.itemName = itemName;
        }

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public static List<ItemMenu> getItem() {
            List<ItemMenu> itemMenus = new ArrayList<>();
            itemMenus.add(new ItemMenu("添加好友", "1"));
            itemMenus.add(new ItemMenu("创建群组", "2"));
            itemMenus.add(new ItemMenu("扫一扫", "3"));
            return itemMenus;
        }
    }
}
